﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;

namespace FindArtInfo.Common
{
    public static class DisplayNameExtensions
    {
        public static IHtmlString MyDisplayNameFor<TModel, TClass, TProperty>
                        (this HtmlHelper<TModel> helper, IEnumerable<TClass> model,
                        Expression<Func<TClass, TProperty>> expression)
        {
            var name = ExpressionHelper.GetExpressionText(expression);
            name = helper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);
            var metadata = ModelMetadataProviders.Current.GetMetadataForProperty(
                () => Activator.CreateInstance<TClass>(), typeof(TClass), name);
            return new MvcHtmlString(metadata.DisplayName);
        }
    }
}